package com.chenfan.process.vo;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.util.StringUtils;

import com.chenfan.process.enums.ProcessResultTypeEnums;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author pengjian
 * @date 2021/1/25 执行过程
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EntireProcessDetailVo {

    /**
     * 执行过程的Id,修改或者查询的需要
     */

    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    private Integer resultType;

    @JsonSerialize(using = ToStringSerializer.class)
    private Long approvalId;

    @ApiModelProperty("状态值")
    private String resultTypeValue;

    public String getResultTypeValue() {
        if (!StringUtils.isEmpty(resultTypeValue)) {
            return resultTypeValue;
        }
        return ProcessResultTypeEnums.getMessage(resultType);
    }

    /**
     * 是否显示按钮（审批中）
     */
    private Boolean showButton;

    /**
     * 执行人的名称
     */
    @ApiModelProperty("审批人名称")
    private String executorUserName;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8")
    private Date createTime;

    @ApiModelProperty("意见")
    private String remark;

    @ApiModelProperty("日志信息")
    private List<ProcessDetailInstance> processInstances;

    /**
     * 是否显示催办按钮
     */
    private Boolean showUrge;

    /**
     * 转让备注
     */
    private String transferRemark;

    public void clearRemark() {
        setRemark(null);
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ProcessDetailInstance {
        @ApiModelProperty("审批人名称")
        private String executorUserName;

        @JsonFormat(pattern = "yyyy-MM-dd HH:mm", timezone = "GMT+8")
        private Date createTime;

        private Integer resultType;

        private String resultTypeValue;

        @ApiModelProperty("意见")
        private String remark;

        /**
         * 转让备注
         */
        private String transferRemark;
    }

    public List<ProcessDetailInstance> transfer() {
        return Collections.singletonList(ProcessDetailInstance.builder().executorUserName(getExecutorUserName())
            .createTime(getCreateTime()).resultTypeValue(getResultTypeValue()).remark(getRemark()).build());
    }
}
